package _1连接数据库;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.注册驱动
       // Class.forName("com.mysql.cj.jdbc.Driver"); //默认可以不写

        //2.获取连接
        String url="jdbc:mysql://127.0.0.1/school";
        String username="root";
        String password="root";
        Connection conn = DriverManager.getConnection(url, username, password);


        //3定义SQL语句

        String sql1="update stu set name='chs'where id=1";
        String sql2="update stu set name='jt'where id=2";

        //4.获取执行SQL对象的Statement
        Statement stmt=conn.createStatement();


        //开启事物


        try {
            //开启事物
            conn.setAutoCommit(false);

            //5.执行SQL
            int result1=stmt.executeUpdate(sql1);

            int p=10/0;
            int result2=stmt.executeUpdate(sql2);
            //6.处理结果
            System.out.println(result1);
            System.out.println(result2);

            conn.commit();
            //提交事物
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }






        //7.释放资源
        stmt.close();
        conn.close();




    }
}
